package com.lovo.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lovo.model.Role;
import com.lovo.model.RoleAuthority;
import com.lovo.model.UserRole;
import com.lovo.service.RoleService;
import com.lovo.util.MyUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class RoleServiceImpl implements RoleService {
    @Override
    public PageInfo<Role> listByPage(int pageNo, int pageSize, Role role) {
        SqlSession sqlSession = MyUtils.getSqlSession();
        String stmt = "com.lovo.dao.RoleMapper.findAllByRoleNameAndRoleDesc";
        PageHelper.startPage(pageNo,pageSize);
        List<Role> list = sqlSession.selectList(stmt, role);
        PageInfo<Role>rolePageInfo = new PageInfo<>(list);
        sqlSession.commit();
        sqlSession.close();
        return rolePageInfo;
    }

    @Override
    public boolean addRole(Role r) {
        SqlSession sqlSession = MyUtils.getSqlSession();
        String stmt = "com.lovo.dao.RoleMapper.insertSelective";
        int n = sqlSession.insert(stmt, r);
        sqlSession.commit();
        sqlSession.close();
        return n>0?true:false;
    }

    @Override
    public boolean deleteRole(int r) {
        SqlSession sqlSession = MyUtils.getSqlSession();
        String stmt = "com.lovo.dao.RoleMapper.deleteByPrimaryKey";
        int n = sqlSession.delete(stmt, r);
        sqlSession.commit();
        sqlSession.close();
        return n>0?true:false;
    }

    @Override
    public boolean updateRole(Role r) {
        SqlSession sqlSession = MyUtils.getSqlSession();
        String stmt = "com.lovo.dao.RoleMapper.updateByPrimaryKeySelective";
        int n = sqlSession.delete(stmt, r);
        sqlSession.commit();
        sqlSession.close();
        return n>0?true:false;
    }

    @Override
    public boolean hasAuthority(int roleId, Integer[] authorityIds) {
        boolean flag = false;
        SqlSession sqlSession = MyUtils.getSqlSession();
        String stmt = "com.lovo.dao.RoleAuthorityMapper.insertSelective";
        String stmt1 = "com.lovo.dao.RoleAuthorityMapper.deleteByFkRoleId";
        sqlSession.delete(stmt1,roleId);
        for (int i = 0; i<authorityIds.length;i++){
            RoleAuthority ra = new RoleAuthority();
            ra.setFkRoleId(roleId);
            ra.setFkAuthorityId(authorityIds[i]);
            int n = sqlSession.insert(stmt, ra);
            if (n>0){
                flag = true;
            }else {
                flag = false;
            }
        }
        sqlSession.commit();
        sqlSession.close();
        return flag;
    }

    @Override
    public int[] getAuthorityIds(int roleId) {
        SqlSession sqlSession = MyUtils.getSqlSession();
        String stmt = "com.lovo.dao.RoleAuthorityMapper.findAllByFkRoleId";
        List<RoleAuthority> roleAuthoritys = sqlSession.selectList(stmt, roleId);
        int [] authorityIds = new int [roleAuthoritys.size()];
        for (int i = 0; i < roleAuthoritys.size(); i++){
            authorityIds[i] = roleAuthoritys.get(i).getFkAuthorityId();
        }
        sqlSession.commit();
        sqlSession.close();
        return authorityIds;
    }

    @Override
    public List<Role> getAllRoles() {
        SqlSession sqlSession = MyUtils.getSqlSession();
        String stmt = "com.lovo.dao.RoleMapper.findAll";
        List<Role> list = sqlSession.selectList(stmt);
        sqlSession.commit();
        sqlSession.close();
        return list;
    }
}
